/**
 * 搜索用户
 */
import { pick } from "lodash";
import { isFriend } from "../../../model/service/chat/getFriendList";
import { searchFriend } from "../../../model/service/chat/searchFriend";
 
 export default async function searchFriends(ctx: Context, next: Next): Promise<void> {
   const { body } = ctx.request;
   const { username, uid } = body;
 
   const result = await searchFriend( username, uid );

   const filterRes = await Promise.all(result.map(async (item: IAnyObject) => {
     const userRelation = await isFriend({ fromUser: item.uid, userId: uid });
     return {
       ...pick(item, ['username', 'uid', 'profile']),
       isFriend: userRelation.length > 0 ? true : false
     }
   }));

   ctx.api.success(filterRes, 'sucess')
   return next();
 }